﻿using DDD.Domain;
using Microsoft.EntityFrameworkCore;
using OrderModule.Domain.OrdeDomainEvent;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderModule.Domain.OrderAggregate
{
    [Comment("订单")]
    [Table("WbOrder")]
    public class Order : Entity<int>, IAggregateRoot
    {
        /// <summary>
        /// 订单号
        /// </summary>
        [Comment("订单号")]
        [Required]
        [StringLength(50)]
        public string OrderNo { get; init; } = string.Empty;

        /// <summary>
        /// 订单状态
        /// </summary>
        [Comment("订单状态")]
        [Required]
        [StringLength(10)]
        public string Status { get; private set; } = string.Empty;

        /// <summary>
        /// 订单金额
        /// </summary>
        [Comment("订单金额")]
        [Required]
        [Precision(9, 2)]
        public decimal Amount { get; private set; } = 0;


        /// <summary>
        /// 订单项
        /// </summary>
        public List<OrderItem> Items { get; private set; } = new List<OrderItem>();

        private Order() { }

        public Order(decimal amount, List<OrderItem> items)
        {
            OrderNo = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
            Status = "待确认";
            Amount = amount;
            Items = items ?? throw new ArgumentNullException(nameof(items));

            this.AddDomainEvent(new OrderCreatedDomainEvent(this));
        }

        public void ChangeStatus(string status)
        {
            if (this.Status != status)
            {
                Status = status;
                //this.AddDomainEvent()
            }
        }
    }
}
